﻿Public Class frmUpdatePersonCID
    Dim mysql As New ClsSQLhelper
    Dim Strsql As String
#Region "Function"
    Sub Load_PPData()
        Try
            Dim DT_Person As New DataTable
            Dim DT_Patient As New DataTable
            Me.lblPersonSQL.Text = "Update Person set cid = '0000000000000' where person_id =  '" & Me.TxtPID.Text & "'"
            With GridPersons
                If TxtPID.Text <> "" Then
                    Strsql = "Select person_id,pname,fname,lname,patient_hn,cid from person where person_id = '" & Me.TxtPID.Text & "'"
                    DT_Person = mysql.GetMYSQLDataTable(Strsql, "patient", True)
                    .RowHeadersVisible = False
                    .AllowDrop = False
                    .AllowUserToAddRows = False
                    .AllowUserToDeleteRows = False
                    .AllowUserToOrderColumns = True
                    .AllowUserToResizeColumns = True
                    .AllowUserToResizeRows = False
                    .MultiSelect = False
                    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                    .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
                    .DataSource = DT_Person
                End If
                With GridPersons
                    Me.TxtHN.Text = .Rows(0).Cells("patient_hn").Value
                    Me.TxtCID.Text = .Rows(0).Cells("cid").Value
                    Me.TxtName.Text = .Rows(0).Cells("pname").Value + .Rows(0).Cells("fname").Value + " " + .Rows(0).Cells("lname").Value
                    Me.OptZeroUpdate.Checked = True
                End With
            End With
            

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
#End Region
    Private Sub BtnForceUpdateZeroCID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnForceUpdateZeroCID.Click
        Try

            MsgBox("การ Update CID แบบไม่ใช้ HOSxP อาจมีความเสี่ยง ผู้ดูแลระบบควรตรวจสอบก่อน update ทุกครั้ง", MsgBoxStyle.Exclamation, "คำเตือน")
            If MsgBox("กรุณาตรวจสอบให้แน่ใจก่อนทำการ Update ว่าไม่มี CID จริงๆ", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "คำเตือน ") = MsgBoxResult.No Then Exit Sub

            If Me.GridPersons.RowCount <> 0 And Me.TxtPID.Text <> "" Then
                Strsql = "Update Person set cid = '"
                If Me.OptZeroUpdate.Checked = True Then
                    Strsql = Strsql & "0000000000000"
                Else
                    Strsql = Strsql & "000000" & Me.TxtHN.Text
                End If
                Strsql = Strsql & "'  where person_id ='" & Me.TxtPID.Text & "'"
                MySql.MySQLExecute(Strsql, True)
            End If

            MsgBox("Update Complete", MsgBoxStyle.Information, "ผลการ update")
            Load_PPData()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnView.Click

        Load_PPData()
    End Sub

    Private Sub TxtPID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtPID.KeyDown
        If e.KeyCode = Keys.Enter Then
            Load_PPData()
        End If
    End Sub

    Private Sub TxtPID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtPID.TextChanged

    End Sub

    Private Sub OptZeroUpdate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptZeroUpdate.CheckedChanged
        Me.lblPersonSQL.Text = "Update Person set cid = '0000000000000' where person_id =  '" & Me.TxtPID.Text & "'"

    End Sub

    Private Sub OptHN_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptHN.CheckedChanged
        Me.lblPersonSQL.Text = "Update Person set cid = '000000" & Me.TxtHN.Text & "' where person_id =  '" & Me.TxtPID.Text & "'"

    End Sub
End Class